/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.fujiyuu75.sequent;

import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;

/**
 * 控件相关工具类
 */
public class ComponentUtil {

    /**
     * 获取控件在父布局中的X坐标
     *
     * @param component 需要查询的控件
     * @return 在父布局中的X坐标
     */
    public static float getXInParent(Component component) {
        int[] los = component.getLocationOnScreen();
        if (component.getComponentParent() != null) {
            ComponentContainer parent = (ComponentContainer) component.getComponentParent();
            int[] los1 = parent.getLocationOnScreen();
            return los[0] - los1[0] - parent.getPaddingLeft();
        }
        return los[0];
    }

    /**
     * 获取控件在父布局中的Y坐标
     *
     * @param component 需要查询的控件
     * @return 在父布局中的Y坐标
     */
    public static float getYInParent(Component component) {
        int[] los = component.getLocationOnScreen();
        if (component.getComponentParent() != null) {
            ComponentContainer parent = (ComponentContainer) component.getComponentParent();
            int[] los1 = parent.getLocationOnScreen();
            return los[1] - los1[1] - parent.getPaddingTop();
        }
        return los[1];
    }

}
